# -*- coding: UTF-8 -*-
import pika
from rabbitmq.MsgWorker import Worker

class MsgUtil:

    def __init__(self):
        credentials=pika.PlainCredentials('yanghui', 'yanghui')
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', credentials=credentials))
        self.worker = Worker()
    
    def __del__(self):
        self.connection.close()
        
    def publish(self, msg):
        channel = self.connection.channel()
        channel.queue_declare(queue='hello', durable=True)
        channel.queue_bind(exchange='exchange', queue="hello", routing_key='sina')
        channel.basic_publish(exchange='exchange', routing_key="sina", body=msg)  
    
    def callback(self, ch, method, properties, body): 
        return body
     
    def receive(self):
        queueName="go"
        exchangeName="followuser"
        ROUTEKEY = "follow:user"
        channel = self.connection.channel()
        channel.queue_declare(queue=queueName, durable=True)
        channel.queue_bind(exchange=exchangeName, queue=queueName, routing_key=ROUTEKEY)
        def callback(ch, method, properties, body):
            self.worker.work(method.routing_key, body)
        channel.basic_consume(callback, queue=queueName, no_ack=True)
        channel.start_consuming()
